package com.it.data_tree.tree;

import com.it.data_tree.util.ColorPrintln;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/04/20/16:32
 * @注释: TODO
 **/
public class TreeNode2 {
    public static void main (String[] args) {
        Node company = new Node("中国电信");
        
        // 添加部门
        Node department1 = new Node("上海应急通信局");
        company.addChild(department1);
        Node department2 = new Node("上海分公司公共服务中心");
        company.addChild(department2);
        
        // 添加办公室
        Node office1 = new Node("办公室");
        department1.addChild(office1);
        Node office2 = new Node("中心领导");
        department2.addChild(office2);
        
        // 添加员工
        Node employee1 = new Node("蒋国银");
        office2.addChild(employee1);
        Node employee2 = new Node("张少杰");
        office2.addChild(employee2);
        
        ColorPrintln.bluePrintln(company.getChildren());
        
        List<Node> children = company.getChildren();
        
        ColorPrintln.cyanPrintln("公司：" + company.name);
        ColorPrintln.pinkPrintln("部门：" + children.get(1).name);
        
        ColorPrintln.greenPrintln("办公室：" + children.get(1).getChildren().get(0).name);
        List<Node> children1 = children.get(1).getChildren().get(0).getChildren();
        
        ColorPrintln.yellowPrintln("所有员工：" + children1.get(0).name + "\t" + children1.get(1).name);
    }
    
    static class Node {
        private String name;  // 节点的名称
        private List<Node> children;  // 子节点列表
        
        // 构造函数
        public Node (String name) {
            this.name = name;
            children = new ArrayList<>();
        }
        
        // 添加子节点
        public void addChild (Node child) {
            children.add(child);
        }
        
        // 删除子节点
        public void removeChild (Node child) {
            children.remove(child);
        }
        
        // 获取子节点列表
        public List<Node> getChildren () {
            return children;
        }
        
        @Override
        public String toString () {
            return "Node{" +
                  "name='" + name + '\'' +
                  ", children=" + children +
                  '}';
        }
    }
}
